<?php
/**
* Prepare graph data
*
* @author       Fabrice Douteaud <admin@clearbudget.net>
* @package      actions
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice Douteaud (admin@clearbudget.net)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/

$report = class_report::getInstance();
$validData = $report->getData();
if($validData === false) {
  $error = 'error_NoTransactionData';
  $backAction = 'tableOverview';
  $nextAction = 'uploadQif';
  $context->snippet = 'errorDisplay';
  return;
  }
  
$xml = new swfChart(swfChart::rootTag);
$xml->test();
echo '<textarea>'.$xml->asXML().'</textarea>';
return;

$dates = $report->dates;
$totalDebits = $report->totalDebits;
$debitsPctPerMonth = $report->debitPctPerMonth;
$creditsPctPerMonth = $report->creditPctPerMonth;
$debitPerCategoryPerMonth = $report->debitPerCategoryPerMonth;
$creditPerCategoryPerMonth = $report->creditPerCategoryPerMonth;
$debitTotalPerMonth = $report->debitTotalPerMonth;
$creditTotalPerMonth = $report->creditTotalPerMonth;
$debitTotalPerCategory = $report->debitTotalPerCategory;
$categories = $report->categories;
$categoryColor = $report->categoryColor;

// Overall credit vs debit graph
$chart1 = new GoogChart();
$creditPct = $report->formatNumber($report->totalCredits/($report->totalDebits+$report->totalCredits) * 100);
$debitPct = $report->formatNumber($report->totalDebits/($report->totalDebits+$report->totalCredits) * 100); 
$dataChart1[$debitPct.'%'] = $debitPct;
$dataChart1[$creditPct.'%'] = $creditPct;
$chart1->setChartAttrs( array(
	'type' => 'pie3d',
	'title' => 'Overall',
	'data' => $dataChart1,
	'size' => array( 250, 100 ),
	'color' => array('#FF2725', '#97EE14'),
	'labelsXY' => true
	));

// Credit vs debit on a monthly basis
$chart2 = new GoogChart();
foreach($dates as $date) {
  $month = explode('-', $date);
  $month = $month[0];
  $dataChart2[$keys->text_Credit][$month]=$creditTotalPerMonth[$date]/($debitTotalPerMonth[$date]+$creditTotalPerMonth[$date]) * 100;
  $dataChart2[$keys->text_Debit][$month]=$debitTotalPerMonth[$date]/($debitTotalPerMonth[$date]+$creditTotalPerMonth[$date]) * 100;
  }
$chart2->setChartAttrs( array(
	'type' => 'bar-vertical2',
	'title' => 'Monthly',
	'data' => $dataChart2,
	'size' => array( 450, 150 ),
	'color' => array('#97EE14', '#FF2725'),
	'labelsXY' => true,
	'fill' => array( '#eeeeee', '#aaaaaa' ),
	));

// top 5 categories overall spending
$chart3 = new GoogChart();
$dataChart3 = array(); 
$dataChart3tmp = array();
foreach($debitTotalPerCategory as $categoryId=>$val) {
  //if($categoryId == 1) continue;
  $dataChart3tmp[$categoryId] = $val;
  }
  
if(count($debitTotalPerCategory) > 0) {
  // sort by value
  asort($debitTotalPerCategory, SORT_NUMERIC);
  // keep only the top 5
  $debitTotalPerCategory = array_slice($debitTotalPerCategory, 0 , 5, true);
  // build the colors array
  foreach($debitTotalPerCategory as $key=>$val) {
    $val = $report->formatNumber($val / $totalDebits * 100);
    $name = $report->getCategoryName($key).' ('.$val.'%)';
    $dataChart3[$name] = $val;
    $colors[] = $categoryColor[$key];
    }

  $chart3->setChartAttrs( array(
  	'type' => 'pie3d',
  	'title' => 'Overall',
  	'data' => $dataChart3,
  	'size' => array( 550, 200 ),
  	'color' => $colors,
  	'labelsXY' => true,
  	'fill' => array( '#eeeeee', '#aaaaaa' ),
  	));
  }
else {
  $chart3->setChartAttrs( array(
  	'type' => 'pie',
  	'title' => 'Overall',
  	'data' => array('100'),
  	'size' => array( 250, 150 ),
  	'color' => array('#000000'),
  	'labelsXY' => true,
  	'fill' => array( '#eeeeee', '#aaaaaa' ),
  	));
  }

// top 5 categories monthly spending
$chart4 = new GoogChart();
$dataChart4 = array(); 
unset($colors);
foreach($debitPerCategoryPerMonth as $categoryId=>$months) {
  //if($categoryId == 1) continue;
  //$colors[] = $categoryColor[$categoryId];
  foreach($months as $month=>$val) {
    $dataChart4[$report->getCategoryName($categoryId)][$month] = $val / $debitTotalPerMonth[$month] * 100;
    $colors[$report->getCategoryName($categoryId)] = $categoryColor[$categoryId];
    //$colors[] = $categoryColor[$categoryId];
    }
  }

if(count($dataChart4) > 0) {
  $chart4->setChartAttrs( array(
  	'type' => 'bar-vertical2',
	  'title' => 'Monthly',
    'data' => $dataChart4,
    'size' => array( 450, 150 ),
    'color' => $colors,
    'labelsXY' => true,
    'fill' => array( '#eeeeee', '#aaaaaa' ),
    ));
  }
else {
  $chart4->setChartAttrs( array(
  	'type' => 'pie',
  	'title' => 'Monthly',
  	'data' => array('100'),
  	'size' => array( 250, 150 ),
  	'color' => array('#000000'),
  	'labelsXY' => true,
  	'fill' => array( '#eeeeee', '#aaaaaa' ),
  	));
  }

?>